﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 税控接口
{
    public partial class FrmUpDate : Form
    {
        public FrmUpDate()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                #region 登陆窗体
                var f = new FrmLogin();
                f.ShowDialog();
                if (ServerConfig.Token.ObjToString().Length == 0)
                { this.Close(); }
                #endregion
                #region 对比本地和服务器的程序版本
                var versionlocal = GetLocalDllVersion();
                var versionserver = GetServerDllVersion();
                if (versionserver.Length > 0 && versionlocal != versionserver)
                {
                    GetServerDll();
                }
                StartSK();
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show("税控接口出现错误："+ ex.Message);
                var filename = GetFileFullName();
                if(System.IO.File.Exists(filename))
                {
                    System.IO.File.Delete(filename);
                }
                this.Close();
                Form1_Load(null, null);
            }

        }

        #region 启动程序
        /// <summary>
        /// 启动程序
        /// </summary>
        private void StartSK()
        {
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.FileName = GetFileFullName();
            startInfo.CreateNoWindow = true;
            startInfo.UseShellExecute = false;
            startInfo.Arguments = ServerConfig.Token + " " + ServerConfig.UserName + " " + ServerConfig.PassWord + " " + ServerConfig.ServerAddress;
            var p = System.Diagnostics.Process.Start(startInfo);
            this.Close();
        }
        #endregion

        #region 获取本地的DLL版本号
        /// <summary>
        /// 获取本地的DLL版本号
        /// </summary>
        /// <returns></returns>
        private string GetLocalDllVersion()
        {
            var filename = GetFileFullName();
            var dcll = Common.cDll.GetDllVersion(filename);
            return dcll;
        }
        #endregion

        #region 获取服务器的DLL版本号
        /// <summary>
        /// 获取服务器的DLL版本号
        /// </summary>
        /// <returns></returns>
        private string GetServerDllVersion()
        {
            var url = string.Format("{0}/api/oms/SK/GetVersion?token={1}",
            ServerConfig.ServerAddress, ServerConfig.Token);
            var s = Common.handleHttp.Get(url);
            var result = Common.handleJSON.DeserializeResult(s);
            if(result.result !=1) { MessageBox.Show(result.resultdetail); return ""; }
            return result.GetVal("Verion").ObjToString();
        }
        #endregion
        private void button1_Click(object sender, EventArgs e)
        {
            StartSK();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            var dcll = GetLocalDllVersion();
            MessageBox.Show(dcll);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            GetServerDll();

        }

        #region 从服务器下载文件
        /// <summary>
        /// 从服务器下载文件
        /// </summary>
        private void GetServerDll()
        {
            var url = GetDownLoadUrl();
            var filename = GetFileFullName();
            var download = new Common.HttpDldFile();
            download.Download(url,filename , this);
        }
        #endregion

        #region 获取下载的URL
        public string GetDownLoadUrl()
        {
            var url = string.Format("{0}/YTSK.exe", ServerConfig.ServerAddress);
            return url;
        }
        #endregion

        private string GetFileFullName()
        {
            var filename = "YTSK.exe";
            var tmp = Environment.GetEnvironmentVariable("temp");
            if (tmp.Substring(tmp.Length - 1, 1) != "\\") { tmp = tmp + "\\"; }
            var fullname = tmp + filename;
            return fullname;

        }

    }
}
